﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;

/// <summary>
/// Language setup for the application
/// </summary>
public partial class LanguageBar : System.Web.UI.UserControl
{
    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			HttpCookie cookie = Request.Cookies["Culture"];
			if (cookie != null)
			{
				this.RadioButtonListLanguages.SelectedValue = cookie.Value;
			}
			else
			{
				this.RadioButtonListLanguages.SelectedValue = DAOUserSettings.GetSettingsForLoggedUser().Language;
				cookie = new HttpCookie("Culture", this.RadioButtonListLanguages.SelectedValue);
				System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(this.RadioButtonListLanguages.SelectedValue);
			}
		}		
    }

    /// <summary>
    /// Changes the language.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
    protected void ChangeLanguage(object sender, EventArgs e)
    {
        HttpCookie cookie = new HttpCookie("Culture");
		cookie.Value = this.RadioButtonListLanguages.SelectedValue;
        Response.Cookies.Add(cookie);
    }

	/// <summary>
	/// Gets the selected language.
	/// </summary>
	/// <returns></returns>
	public string GetSelectedLanguage()
	{
		HttpCookie cookie = Request.Cookies["Culture"];
		if (cookie != null)
		{
			return cookie.Value;
		}
		else
		{
			return System.Threading.Thread.CurrentThread.CurrentCulture.Name;
		}
	}
}
